package com.longtop.leaning.dao;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import com.longtop.leaning.data.AppData;
import com.longtop.leaning.entity.User;

public class UserHTTPDao implements UserDao {

	private static final UserHTTPDao instance = new UserHTTPDao();

	private UserHTTPDao() {
	}

	public static UserHTTPDao getInstance() {
		return instance;
	}

	public void test() {
		try {
			String content = XMLUtil.getContentFromURL("http://download.eclipse.org/dsdp/mtj/updates/1.1.1/stable/");
			System.out.println(content);
			XMLUtil.testParseSampleXML(content);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void postViaHttpConnection(String url) throws IOException {
		HttpConnection c = null;
		InputStream is = null;
		OutputStream os = null;
		int rc;

		try {
			c = (HttpConnection) Connector.open(url);

			// Set the request method and headers
			c.setRequestMethod(HttpConnection.POST);
			c.setRequestProperty("If-Modified-Since", "29 Oct 1999 19:43:31 GMT");
			c.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
			c.setRequestProperty("Content-Language", "en-US");

			// Getting the output stream may flush the headers
			os = c.openOutputStream();
			os.write("LIST games\n".getBytes());
			os.flush(); // Optional, getResponseCode will flush

			// Getting the response code will open the connection,
			// send the request, and read the HTTP response headers.
			// The headers are stored until requested.
			rc = c.getResponseCode();
			if (rc != HttpConnection.HTTP_OK) {
				throw new IOException("HTTP response code: " + rc);
			}

			is = c.openInputStream();

			// Get the ContentType
			String type = c.getType();
			//processType(type);
			System.out.println(type);

			// Get the length and process the data
			int len = (int) c.getLength();
			if (len > 0) {
				int actual = 0;
				int bytesread = 0;
				byte[] data = new byte[len];
				while ((bytesread != len) && (actual != -1)) {
					actual = is.read(data, bytesread, len - bytesread);
					bytesread += actual;
				}
				//process(data);
				
			} else {
				int ch;
				while ((ch = is.read()) != -1) {
					//process((byte) ch);
				}
			}
		} catch (ClassCastException e) {
			throw new IllegalArgumentException("Not an HTTP URL");
		} finally {
			if (is != null)
				is.close();
			if (os != null)
				os.close();
			if (c != null)
				c.close();
		}
	}

	public int addUser(User user) {
		throw new RuntimeException("no supported operation!");
	}

	public User getUser(String loginid, String passwd) {
		String content = XMLUtil.getContentFromURL(AppData.SERVER_URL+"user_login.do?userno="+loginid+"&passwd="+passwd);
		if("1".equals(content.trim())){//登录成功
			User user = new User();
			user.setLoginid(loginid);
			user.setPasswd(passwd);
			return user;
		}
		return null;
	}

	public void clearUser() {
		throw new RuntimeException("no supported operation!");
	}

}
